local tuomou = fk.CreateSkill{
  name = "hanqing__tuomou",
}

Fk:loadTranslationTable{
  ["hanqing__tuomou"] = "托谋",
  [":hanqing__tuomou"] = "每轮开始时，你可以将一名角色本轮座次调整为末位，若如此做，其回合开始时获得你所有手牌并明置之"..
  "（其本回合使用这些牌无距离与次数限制），其摸等量张牌；该回合结束时，若有角色于该回合受到伤害但未死亡，你进行一次濒死结算。",

  ["#hanqing__tuomou-choose"] = "托谋：你可以将一名角色本轮调整至末位",
  ["@hanqing__tuomou-round"] = "托谋",
  ["@@hanqing__tuomou-inhand-turn"] = "托谋",
}

local DIY = require "packages/diy_utility/diy_utility"

tuomou:addEffect(fk.RoundStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(tuomou.name)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = room.alive_players,
      skill_name = tuomou.name,
      prompt = "#hanqing__tuomou-choose",
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    room:setPlayerMark(player, "@hanqing__tuomou-round", Fk:translate(to.general))
    room:setPlayerMark(player, "hanqing__tuomou-round", to.id)
    local orig = to.seat
    if orig < #room.players then
      room:moveSeatTo(to, #room.players)
      room.logic:getCurrentEvent():findParent(GameEvent.Round, true):addCleaner(function()
        room:moveSeatTo(to, orig, false)
      end)
    end
  end,
})

tuomou:addEffect(fk.TurnStart, {
  anim_type = "support",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:getMark("hanqing__tuomou-round") == target.id and
      not player.dead and not target.dead and not player:isKongcheng()
  end,
  on_cost = function (self, event, target, player, data)
    event:setCostData(self, {tos = {target}})
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "hanqing__tuomou_invoke-turn", 1)
    local cards = player:getCardIds("h")
    if target == player then
      for _, id in ipairs(cards) do
        room:setCardMark(Fk:getCardById(id), "@@hanqing__tuomou-inhand-turn", 1)
      end
    else
      room:moveCardTo(cards, Card.PlayerHand, target, fk.ReasonGive, tuomou.name, nil, false, player, "@@hanqing__tuomou-inhand-turn")
    end
    if target.dead then return end
    local ids = table.filter(cards, function (id)
      return table.contains(target:getCardIds("h"), id)
    end)
    if #ids > 0 then
      DIY.showCards(target, ids)
    end
    if target.dead then return end
    target:drawCards(#cards, tuomou.name)
  end,
})

tuomou:addEffect(fk.TurnEnd, {
  anim_type = "negative",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:getMark("hanqing__tuomou-round") == target.id and
      player:getMark("hanqing__tuomou_invoke-turn") > 0 and
      not player.dead and
      #player.room.logic:getActualDamageEvents(1, function (e)
        return not e.data.to.dead
      end, Player.HistoryTurn) > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local orig_hp = player.hp
    player.hp = math.min(player.hp, 0)
    room:broadcastProperty(player, "hp")
    room:broadcastPlaySound("./audio/system/losehp")
    room:enterDying({
      who = player,
    })
    if not player.dead then
      player.hp = math.min(player.hp + orig_hp - 1, player.maxHp)
      room:broadcastProperty(player, "hp")
      if player.hp < 1 then
        room:enterDying({
          who = player,
        })
      end
    end
  end,
})

tuomou:addEffect("targetmod", {
  bypass_times = function(self, player, skill, scope, card, to)
    return card and card:getMark("@@hanqing__tuomou-inhand-turn") > 0
  end,
  bypass_distances =  function(self, player, skill, card, to)
    return card and card:getMark("@@hanqing__tuomou-inhand-turn") > 0
  end,
})

return tuomou
